package com.hmall.item.web;

import com.hmall.common.dto.PageDTO;
import com.hmall.item.constantnts.MQConstants;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/list")
    public PageDTO<Item> page(Long page, Long size) {

        PageDTO<Item> pageDTO = itemService.itemPage(page, size);

        return pageDTO;
    }

    @GetMapping("{id}")
    public Item selectById(@PathVariable("id") Long itemId) {
        Item item = itemService.getById(itemId);
        return item;
    }

    @PostMapping
    public void add(@RequestBody Item item) {
        itemService.add(item);
        //发送增加或者修改信息
        rabbitTemplate.convertAndSend(MQConstants.HMALL_EXCHANGE,MQConstants.HMALL_INSERT_KEY,item.getId());
    }

    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable("id") Long itemId, @PathVariable("status") Integer itemStatus) {
        itemService.updateStatusById(itemId, itemStatus);
        //发送增加或者修改信息
        rabbitTemplate.convertAndSend(MQConstants.HMALL_EXCHANGE,MQConstants.HMALL_INSERT_KEY,itemId);
    }

    @PutMapping
    public void updateItem(@RequestBody Item item){
        itemService.updateById(item);
        //发送增加或者修改信息
        rabbitTemplate.convertAndSend(MQConstants.HMALL_EXCHANGE,MQConstants.HMALL_INSERT_KEY,item.getId());
    }

    @DeleteMapping("{id}")
    public void deleteById(@PathVariable("id") Long itemId){
        itemService.deleteById(itemId);
        //发送删除信息
        rabbitTemplate.convertAndSend(MQConstants.HMALL_EXCHANGE,MQConstants.HMALL_DELETE_KEY,itemId);
    }

}
